package com.atguigu.ssyx.home.service.impl;


import com.atguigu.ssyx.client.activity.ActivityFeignClient;
import com.atguigu.ssyx.client.product.ProductFeignClient;
import com.atguigu.ssyx.client.search.SearchFeignClient;
import com.atguigu.ssyx.home.service.ItemService;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @作者:胡树苑
 * @时间:2025/9/21 20:32
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ThreadPoolExecutor threadPoolexecutor;
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ActivityFeignClient activityFeignClient;
    @Autowired
    private SearchFeignClient searchFeignClient;
    //获取商品详情
    @Override
    public Map<String, Object> getItem(Long skuId, Long userId) {
        Map<String, Object> result = new HashMap<>();
        //skuId查询信息
        CompletableFuture<SkuInfoVo> skuInfocompletableFuture = CompletableFuture.supplyAsync(() -> {
            //远程调用获取sku对应数据
            SkuInfoVo skuInfoVo = null;
            skuInfoVo=productFeignClient.getSkuInfoVo(skuId);

            result.put("skuInfoVo", skuInfoVo);
            return skuInfoVo;
        }, threadPoolexecutor);
        //sku对应的优惠劵信息
        CompletableFuture<Void> activitycompletableFuture = CompletableFuture.runAsync(() -> {
           //远程调用获取优惠卷
           Map<String,Object> activitymap =
                   activityFeignClient.findActivityAndCoupon(skuId, userId);
           result.putAll(activitymap);


        }, threadPoolexecutor);
        //更新商品热度
        CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
        //远程调用更新热度
            searchFeignClient.incrHotScore(skuId);
        }, threadPoolexecutor);
        //任务组合
        CompletableFuture.allOf(
                skuInfocompletableFuture,
                activitycompletableFuture,
                hotCompletableFuture
        ).join();
        return result;
    }
}
